﻿using System;
using StockLib;
using System.Windows.Forms;
using StockUtil;

namespace StockUI
{
    public partial class DialogStockBlockRename : DialogAbstract
    {
        public StockBlock StockBlock { get; set; }

        public DialogStockBlockRename()
        {
            InitializeComponent();
        }

        private void DialogStockBlockRename_Load(object sender, EventArgs e)
        {
            if (StockBlock == null)
            {

                throw new Exception("StockDomain对象为空");
            }

            this.Text += " - " + StockBlock.Name;

            txtName.Text = StockBlock.Name;
            txtShortName.Text = StockBlock.ShortName;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            txtName.Text = txtName.Text.Trim();
            txtShortName.Text = txtShortName.Text.Trim();

            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show(this, "版块名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            bool repeated = false;
            foreach (StockBlock domain in StockBlockGroupCustom.Instance.Values)
            {
                if (domain != StockBlock && domain.Name == txtName.Text)
                {
                    repeated = true;
                    break;
                }
            }
            if (repeated)
            {
                MessageBox.Show(this, "版块名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            StockBlock.Name = txtName.Text;
            StockBlock.ShortName = txtShortName.Text;
            StockBlockGroupCustom.Instance.RaiseDataChanged(sender, e);

            DialogResult = DialogResult.OK;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            txtShortName.Text = StringHelper.GetPinYin(txtName.Text).ToUpper();
        }
    }
}
